package com.anjie.powerproject.strategy.pojo;

import com.anjie.powerproject.strategy.pojo.device.BaseDevice;
import com.anjie.powerproject.strategy.pojo.device.StorageDevice;
import com.anjie.powerproject.strategy.pojo.device.SupplyDevice;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 策略组合类
 */
public class StrategyComb {
    private Integer code;
    private List<Strategy> strategyList;

    /**
     * 单策略，使用strategy的code
     * @param strategy
     */
    public StrategyComb(Strategy strategy) {
        this.code = strategy.getCode();
        this.strategyList = new ArrayList<>();
        this.strategyList.add(strategy);
    }

    /**
     * 组合策略，需要重定义code
     * 注意：此处strategy传递的顺序代表着单个策略的优先级，排在前的优先级高
     * @param code
     * @param strategy
     */
    public StrategyComb(Integer code, Strategy ...strategy) {
        this.code = code;
        this.strategyList = new ArrayList<>();
        this.strategyList.addAll(Arrays.asList(strategy));
    }

    public Integer getCode() {
        return code;
    }

    public List<Strategy> getStrategyList() {
        return strategyList;
    }

    public void setFlagAllBindingDevice(int start, int end) {
        Strategy strategy = null;
        BaseDevice device = null;
        for (int k=0; k<strategyList.size(); k++) {
            strategy = strategyList.get(k);
            device = strategy.getDevice();
            if (device instanceof SupplyDevice && strategy.getType() == 1) {
                SupplyDevice supplyDevice = (SupplyDevice) device;
                int[] flag = supplyDevice.getFlag();
                int len = flag.length;
                if (start > end) {
                    for (int i = start; i<len; i++) {
                        flag[i] = 1;
                    }
                    start = 0;
                }
                for (int i = start; i<=end; i++) {
                    flag[i] = 1;
                }
                supplyDevice.setFlag(flag);
            }
            if (device instanceof StorageDevice && strategy.getType() == 2) {
                SupplyDevice supplyDevice = (SupplyDevice) device;
                int[] flag = supplyDevice.getFlag();
                int len = flag.length;
                if (start > end) {
                    for (int i = start; i<len; i++) {
                        flag[i] = 2;
                    }
                    start = 0;
                }
                for (int i = start; i<=end; i++) {
                    flag[i] = 2;
                }
                supplyDevice.setFlag(flag);
            }
        }
    }
}